﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using DAL;
using Model;
using System.Data;
using System.Collections;

namespace BLL
{
    public class UserManage
    {
        //获取所有用户
        public ArrayList GetALLUsers(string sql)
        {
            try
            {
                UserService memberdal = new UserService();
                return memberdal.GetALLUsers(sql);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
        //用户身份验证
        public int CheckUser(string uname, string pwd, string role)
        {
            try
            {
                UserService userdal = new UserService();
                User user = userdal.GetUserByName(uname);
                if (user != null)
                {
                    if (user.Pwd == pwd && user.Role == role)
                    {
                        return 1;
                    }
                    else
                    {
                        return 2;
                    }
                }
                else
                {
                    return 3;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

        //修改密码用户验证
        public int CheckUptUser(string uname, string pwd)
        {
            try
            {
                UserService userdal = new UserService();
                User user = userdal.GetUserByName(uname);
                if (user != null)
                {
                    if (user.Pwd == pwd )
                    {
                        return 1;
                    }
                    else
                    {
                        return 2;
                    }
                }
                else
                {
                    return 3;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
        
        //根据用户名获取用户对象
        public User GetUserByName(string name)
        {
            try
            {
                UserService userdal = new UserService();
                User user = userdal.GetUserByName(name);
                return user;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

        public bool UpdateUsers(User user)//修改用户信息（密码）
        {
            try
            {
                UserService userdal = new UserService();
                return userdal.UpdateUser(user);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }

        }

        public bool AddUsers(User user)//注册用户信息（密码）
        {
            try
            {
                UserService userdal = new UserService();
                return userdal.AddUser(user);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }

        }

        public bool DelUsers(User user)
        {
            try
            {
                UserService userdal = new UserService();
                return userdal.DelUsers(user);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

        public bool UptUsers(User user)
        {
            try
            {
                UserService userdal = new UserService();
                return userdal.UptUser(user);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
    }
}
